package com.ecoolex.discount.sales.ws;

import java.time.Instant;
import java.util.*;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.ecoolex.discount.sales.core.manager.YouWorldOrderRecordManager;
import com.ecoolex.discount.sales.core.manager.YwGiftOrderDeviceRecordManager;
import com.ecoolex.discount.sales.entity.YouWorldOrderRecord;
import com.ecoolex.discount.sales.pojo.YouWorldOrderRecordRequest;
import com.ecoolex.discount.sales.pojo.YouWorldOrderStatusUpdateRequest;
import com.ecoolex.discount.sales.pojo.YwGiftOrderDeviceRecordDTO;

@Service
public class YouWorldOrderRecordServiceImpl implements YouWorldOrderRecordService {
	@Autowired
	YouWorldOrderRecordManager youWorldOrderRecordManager;

	static final String SYSTEM_USER = "superadmin";

	@Override
	public void record(YouWorldOrderRecordRequest request, String ip) {
		Optional<YouWorldOrderRecord> optional = youWorldOrderRecordManager.getByOrderNo(request.getOrderNo());
		if (!optional.isPresent()) {
			YouWorldOrderRecord record = new YouWorldOrderRecord();
			BeanUtils.copyProperties(request, record);
			record.setStatus(1);
			record.setDeleted(YouWorldOrderRecord.NOT_DELETED);
			Date now = Date.from(Instant.now());
			record.setCreateUser(Optional.ofNullable(ip).orElse(SYSTEM_USER));
			record.setCreateTime(now);
			record.setUpdateUser(SYSTEM_USER);
			record.setUpdateTime(now);
			youWorldOrderRecordManager.save(record);
		}

	}

	@Override
	public void update(YouWorldOrderStatusUpdateRequest request, String ip) {
		if (Objects.nonNull(request) && !CollectionUtils.isEmpty(request.getData())) {
			request.getData().forEach(item -> {
				Optional<YouWorldOrderRecord> optional = youWorldOrderRecordManager.getByOrderNo(item.getOrderNo());
				if (optional.isPresent()) {
					YouWorldOrderRecord record = optional.get();
					List<YouWorldOrderRecord.Column> changed = new ArrayList<>();
					if (StringUtils.hasText(item.getPaymentStatus())) {
						record.setPaymentStatus(item.getPaymentStatus());
						changed.add(YouWorldOrderRecord.Column.paymentStatus);
					}
					if (StringUtils.hasText(item.getRefundStatus())) {
						record.setRefundStatus(item.getRefundStatus());
						changed.add(YouWorldOrderRecord.Column.refundStatus);
					}
					if (!CollectionUtils.isEmpty(changed)) {
						record.setUpdateUser(Optional.ofNullable(ip).orElse(SYSTEM_USER));
						record.setUpdateTime(Date.from(Instant.now()));
						changed.add(YouWorldOrderRecord.Column.updateUser);
						changed.add(YouWorldOrderRecord.Column.updateTime);
						YouWorldOrderRecord.Column[] columns = new YouWorldOrderRecord.Column[changed.size()];
						youWorldOrderRecordManager.upate(record, changed.toArray(columns));
					}
				}
				else {
					YouWorldOrderRecord record = new YouWorldOrderRecord();
					BeanUtils.copyProperties(item, record);
					record.setStatus(1);
					record.setDeleted(YouWorldOrderRecord.NOT_DELETED);
					Date now = Date.from(Instant.now());
					record.setCreateUser(Optional.ofNullable(ip).orElse(SYSTEM_USER));
					record.setCreateTime(now);
					record.setUpdateUser(SYSTEM_USER);
					record.setUpdateTime(now);
					youWorldOrderRecordManager.save(record);
				}

			});
		}

	}
	
	@Autowired
	private YwGiftOrderDeviceRecordManager ywGiftOrderDeviceRecordManager;

	@Override
	public void userDeviceInfoRecord(YwGiftOrderDeviceRecordDTO record,String ip,String createUser) {
		ywGiftOrderDeviceRecordManager.userDeviceInfoRecord(record, ip, createUser);
	}
}
